import { _decorator, Component, Node,instantiate,Prefab, input, Input} from 'cc';
import { ResourcesManager } from './ResourcesManager';
import { Tower } from './Tower';
import { GameManager } from './GameManager';
const { ccclass, property } = _decorator;

enum TowerType {
    NONE,
    ARROW,
    BOMB
}

@ccclass('TowerManager')
export class TowerManager extends Component {


    @property(Prefab)
    tower:Prefab = null;

    @property(Prefab)
    arrowTower:Prefab = null;

    @property(Prefab)
    bombTower:Prefab = null;

    //当前关卡
    currStage = 0;

    private currSelectedTower:Node = null;

    private static _instance: TowerManager = null;

    gameManager:GameManager = null;

    //单例
    public static get instance(): TowerManager {
        return this._instance;
    }


    protected onLoad(): void {
        TowerManager._instance = this;
    }

    start() {

        input.on(Input.EventType.TOUCH_START,this.screenTouchStart,this);
        this.gameManager = GameManager.getInstance();

        this.createTowers();
    }

    update(deltaTime: number) {
        
    }

    //屏幕点击时 隐藏塔升级
    screenTouchStart(){
        if(this.currSelectedTower){
            this.currSelectedTower.getComponent(Tower).hideUpdate();
            this.currSelectedTower = null;
        }
    }

    //创建塔
    async createTowers() {

        var stageInfo = await this.getStageInfo();

        for(var item of stageInfo[this.currStage].towers){
            var tower = instantiate(this.tower);
            tower.setParent(this.node);

           tower.setPosition(item.position[0],item.position[1],0);
        }
    }

    //获取关卡信息
    async getStageInfo() {
        try {
            const data = await ResourcesManager.instance.loadJson("levelData/TowersPos");   
            return data.json.info;
        } catch (err) {
            console.error("加载失败:", err);
        }
    }

    
    //防御塔的升级状态显示或隐藏
    handleTowerClick(tower:Node){
        //如果当前选中的塔不为空，则隐藏其升级状态
        if(this.currSelectedTower){
            this.currSelectedTower.getComponent(Tower).hideUpdate();
        }
        //设置当前选中的塔
        this.currSelectedTower = tower;
        //显示当前选中的塔的升级状态
        this.currSelectedTower.getComponent(Tower).showUpdate();
    }
    

    //处理防御塔升级
    handleTowerChoice(tower:Node){

        //创建防御塔时通知GameManager
        var isSuccess = this.gameManager.createTower(tower);
        if(!isSuccess){
            return null;
        }

        var newTower = null;
        switch(tower.getComponent(Tower).type){
            //选择了箭塔
            case TowerType.ARROW:
                newTower = instantiate(this.arrowTower)
                break;
            //选择了炸弹塔
            case TowerType.BOMB:
                newTower = instantiate(this.bombTower)
                break;
        }

        var pos = tower.getPosition();
        tower.destroy();
        newTower.setParent(this.node);
        newTower.setPosition(pos);
        this.currSelectedTower = newTower;
        return newTower;
    }
}


